package com.bgs.travelapp.comment.service.impl;


import com.bgs.travelapp.comment.eneity.CommentEntity;
import com.bgs.travelapp.comment.mapper.CommentMapper;
import com.bgs.travelapp.comment.service.CommentService;
import com.bgs.travelapp.page.MyPageHelper;
import com.bgs.travelapp.page.PageRequest;
import com.bgs.travelapp.page.PageResult;
import com.bgs.travelapp.scenic.entity.ScenicEntity;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

/**
 * 前端评论功能（前端）
 * hbx 2022年8月10日11:40:20
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    CommentMapper commentMapper;

    /**
     * 新增评论（前端）
     * hbx 2022年8月10日17:00:28
     * @param commentEntity
     * @return
     */
    @Override
    public Integer saveComment(CommentEntity commentEntity) {

        commentEntity.setCreateTime(new Date());
        commentEntity.setDelFlag(0);
        commentEntity.setParentId(0);
        return commentMapper.insertSelective(commentEntity);
    }

    /**
     * 根据评论id删除评论
     * hbx 2022年8月11日11:27:47
     * @param id
     * @return
     */
    @Override
    public Integer deleteCommentById(Integer id) {
        CommentEntity commentEntity = new CommentEntity();
        commentEntity.setId(id);
        commentEntity.setDelFlag(1);
        int i = commentMapper.updateByPrimaryKeySelective(commentEntity);
        return i;
    }

    /**
     * 根据游记id查询游记相关的评论
     * hbx 2022-8-17 08:50:27
     * @param travelsId
     * @return
     */
    @Override
    public List<CommentEntity> getCommentAllByTravesId(Integer travelsId) {
        List<CommentEntity> commentEntityList = commentMapper.getSonCommentAll(travelsId);
        List<CommentEntity> newCommentEntity = new ArrayList<>();
        for(CommentEntity commentEntity:commentEntityList){
            if(commentEntity.getParentId() == 0){
                   newCommentEntity.add(getChild(commentEntity,commentEntityList));
            }
        }

//        MyPageHelper.startPage(pageRequest);
//        PageResult pageEntity = MyPageHelper.pageInfo(newCommentEntity);
//        return pageEntity;
        return newCommentEntity;
    }


    /**
     * 查询评论的递归
     * hbx 2022-8-17 08:51:39
     * @param commentEntity
     * @param commentEntityList
     * @return
     */
    private CommentEntity getChild(CommentEntity commentEntity, List<CommentEntity> commentEntityList) {
        List<CommentEntity> newCommentEntity = new ArrayList<>();
        for(CommentEntity childComment:commentEntityList){
            if(commentEntity.getId().equals(childComment.getParentId())){
                newCommentEntity.add(getChild(childComment,commentEntityList));
            }
        }
        commentEntity.setChilds(newCommentEntity);
        return commentEntity;
    }

    /**
     * 新增回复评论 （前端）
     * hbx 2022年8月16日20:25:58
     * @param commentEntity
     * @return
     */
    @Override
    public Integer saveReplyFirst(CommentEntity commentEntity) {
        commentEntity.setCreateTime(new Date());
        commentEntity.setDelFlag(0);
        return commentMapper.insertSelective(commentEntity);
    }

    /**
     * 新增二级回复评论 （前端）
     * hbx 2022年8月16日20:25:01
     * @param commentEntity
     * @return
     */
    @Override
    public Integer saveReplySecond(CommentEntity commentEntity) {
        commentEntity.setCreateTime(new Date());
        commentEntity.setDelFlag(0);
        return commentMapper.insertSelective(commentEntity);
    }

    /**
     * 分页 获取游记评论信息（后端）
     * hbx 2022年8月18日10:06:48
     * @param pageRequest
     * @return
     */
    @Override
    public PageResult getCommentAll(PageRequest pageRequest) {
        Example example=new Example(CommentEntity.class);
        example.createCriteria().andEqualTo("delFlag",0);
        MyPageHelper.startPage(pageRequest);
        List<CommentEntity> commentEntityList =commentMapper.selectByExample(example);
        PageResult pageEntity = MyPageHelper.pageInfo(commentEntityList);
        return pageEntity;
    }

    /**
     * 分页 根据用户名称，游记名称查询评论信息
     * @param pageRequest
     * @param userName
     * @param travelsName
     * @return
     */
    @Override
    public PageResult findCommentByCondition(PageRequest pageRequest, String userName, String travelsName,String createTime) {
        Example example = new Example(CommentEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("createTime","%"+createTime+"%").andLike("travelsName","%"+travelsName+"%").andLike("userName","%"+userName+"%").andEqualTo("delFlag",0);
        MyPageHelper.startPage(pageRequest);
        List<CommentEntity> commentEntityList = commentMapper.selectByExample(example);
        PageResult pageEntity = MyPageHelper.pageInfo(commentEntityList);
        return pageEntity;
    }
}
